import *as imageService from "./service";
export default {
  namespace:"docker/image",
  state:{
    image:[],
    selectKeys:[],
    imageInfo:[]
  },
  subscriptions:{
    setup({dispatch,history}){
      history.listen((location)=>{
        if(location.pathname==='/docker/image'){
          dispatch({type:'query'});
        }
      })
    }
  },
  effects:{
    * query({payload},{call,put}){
      const {data} = yield call(imageService.getImageList);
      yield put({type:'update',payload:{image:data.data}});
    },
    * selectKeys({payload},{call,put}){
      yield put({type:'update',payload:{selectKeys:payload.selectedRowKeys}});
    },
    * remove({},{call,put,select}){
      const image = yield select((_)=>({image:_['docker/image']}));
      const {data}=yield call(imageService.remove,image.image.selectKeys);
      if(data.code===200) {
        yield put({ type: 'query'});
      }
    },
    * imageInfo({payload},{call,put,select}){
      const {data} = yield call(imageService.getImageInfo,payload.record.id);
      yield put({type:'update',payload:{imageInfo:data.data}});
    }
  },
  reducers: {
    update(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  }
}
